package jp.sourceforge.akjrcp.potrace;


import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;

import org.freehep.postscript.PSDevice;

public class FreeHepGraphicsDevice extends PSDevice{
	int w;
	int h;
	Graphics2D imageGraphics;
	
	public BufferedImage image;
	private Graphics2D graphics;
	public FreeHepGraphicsDevice(Graphics2D g2,int w,int h){
	this.w=w;
	this.h=h;
	
	graphics=g2;
	//graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
	//graphics.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
	//graphics.scale(2.0,2.0);
	}
    public double getWidth() {
        return w;
    }
    
    public double getHeight() {
        return h;
    }
      
    public AffineTransform getDeviceTransform() {
    	return graphics.getTransform();
        //return panel.getGraphicsConfiguration().getDefaultTransform();  
    }
        
    public Graphics getDeviceGraphics() {
    	//log.info("getDeviceGraphics");
        return graphics;
    }

    public Graphics2D getGraphics() {
    	
        if (imageGraphics != null){
        	return imageGraphics;
        }
        return super.getGraphics();
    }

    public void refresh() {
       // log.inforefresh");
    }
    
    public BufferedImage convertToImage(int width, int height) {
        image = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);
        imageGraphics = (Graphics2D)image.getGraphics();
        return image;
    }
	public BufferedImage getImage() {
		return image;
	}
	public void setImage(BufferedImage image) {
		this.image = image;
	}
	
    public void erasePage() {
    	 //super.erasePage();
    	// log.info("erasePage");
    	 graphics = (Graphics2D)getDeviceGraphics();
        
         graphics.setBackground(Color.red);
         //graphics.fillRect(0, 0, (int)getWidth(), (int)getHeight());
         valid=false;
    }

}
